layui.extend({
    selectN: '/layui_extends/selectN',
    selectM: '/layui_extends/selectM',
    baseConfig: "/js/pages/baseConfig"
}).use(['form', 'layer', 'table', 'element', 'util', 'baseConfig'], function () {

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;

    var hideCol = true;
    if (baseConfig.checkPrivalege(baseConfig.CONSUMER_LIST_ALL)) {
        hideCol = false;
    }
    var cooperativeName = getUrlParamer("cooperativeName");
    var jobNumber = getUrlParamer("jobNumber");
    var id = getUrlParamer("id");
    var tableIns = table.render({
        elem: '#list',
        url: '/consumer/listForCloudDisk?cooperativeSimpleName=' + cooperativeName + "&jobNumber=" + jobNumber,
        cellMinWidth: 95,
        height: "full",
        text: {none: "Aucun résultat"},
        //       toolbar: true, //开启头部工具栏，并为其绑定左侧模板
        //      defaultToolbar: ['filter'],
        totalRow: true, //开启合计行
        id: "listTable",
        cols: [[
            {type: "radio"},
            {
                field: 'dateDeDepense', title: "Date de dépense", align: "center", templet: function (d) {
                    return baseConfig.formatDate(d.dateDeDepense);
                }
            },
            {
                field: 'invoiceReviewTime',
                hide: hideCol,
                title: "Date de control",
                align: "center",
                templet: function (d) {
                    if (d.invoiceReviewTime) {
                        return baseConfig.formatDate(d.invoiceReviewTime);
                    } else {
                        return "/";
                    }
                }
            },
            {
                field: 'cooperativeSimpleName',
                title: "Fournisseur",
                width: 450,
                align: "center",
                templet: function (d) {
                    return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.cooperativeSimpleName + "</div>";
                }
            },
            {field: 'jobNumber', title: "Collaborateurs", align: "center"},
            {
                field: 'carteIdentifiant', title: "Idantifiant", align: "center", templet: function (d) {
                    if (d.sourceFrom == "SYNC_FROM_DEPANPERSO_TRIGGER") {//如果消费数据是来自 depan perso的数据，则这里显示 methode
                        return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">NON CARTE</div>";
                    } else {
                        return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.carteIdentifiant + "</div>";
                    }
                }
            },
            {
                field: 'montantTTC',
                title: "Montant TTC",
                align: "center",
                totalRow: true,
                totalRowText: "总计：",
                templet: function (d) {
                    var str = parseFloat(d.montantTTC).toFixed(2) + " €";
                    return "<div style='width: 40%;text-align: right;margin-right: 30%'>" + str + "</div>"
                }
            }
        ]],
        response: {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.count, //数据条数
                "data": res.data.data //解析数据列表
            };
        }
    });

    form.on("submit(submitBtn)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        var checkStatus = table.checkStatus('listTable'),
            data = checkStatus.data;
        var consumerId = data[0].id;

        //提交数据
        $.post("/cloudDisk/handBindConsumer", {
            otherId: consumerId,
            id: id
        }, function (res) {
            if (res.code == 200) {
                setTimeout(function () {
                    top.layer.close(index);
                    top.layer.msg("绑定消费记录成功");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }, 500);
            } else {
                top.layer.close(index);
                top.layer.msg(res.msg);
            }
        });
    });
});
